//
//  LookMoreTopicViewController.m
//  Beauty
//
//  Created by Snow on 16/9/14.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "LookMoreTopicViewController.h"
#import "HotTopicCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfHotTopic.h"
#import "HotTopicViewController.h"
@interface LookMoreTopicViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrayOfHotTopic;

@end

@implementation LookMoreTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.minimumLineSpacing = 10;//横向间距| || |
    self.layout.minimumInteritemSpacing = 10;
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    self.collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[HotTopicCollectionViewCell class] forCellWithReuseIdentifier:@"HotTopicCollectionViewCell"];
    self.layout.itemSize = CGSizeMake((SCREEN_WIDTH - 45) / 3, 160);
}
- (void)handleData
{
    _arrayOfHotTopic = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=index&op=special_list_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"hot_topics"];
        for (NSDictionary *dic in array) {
            ModelOfHotTopic *model = [[ModelOfHotTopic alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfHotTopic addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfHotTopic.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotTopicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotTopicCollectionViewCell" forIndexPath:indexPath];
    ModelOfHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.class_image]];
    cell.labelOfTopic.text = model.class_name;
    cell.labelOfTopic.textAlignment = 1;
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 5;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    HotTopicViewController *hot = [[HotTopicViewController alloc] init];
    hot.class_id = model.class_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:hot animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
